import { Tween, Vec3, Node, tween, v3 } from "cc";

export class TweenMgr {
    private static instance: TweenMgr;
    private constructor() { }
    public static getInstance(): TweenMgr {
        if (this.instance == null) {
            this.instance = new TweenMgr();
        }
        return this.instance;
    }
    private twenMap: Map<string, Tween<any>> = new Map();

    public getTween(node: Node): Tween<any> {
        if (this.twenMap.has(node.uuid)) {
            this.stopTween(node)
        }
        let tween2 = tween(node)
        this.twenMap.set(node.uuid, tween2)
        return tween2;
    }
    public stopTween(node: Node) {
        if (this.twenMap.has(node.uuid)) {
            this.twenMap.get(node.uuid).stop();
            this.twenMap.delete(node.uuid)
        }
    }
    /**心跳动画 */
    playHeartAni(node: Node) {
        let tween = this.getTween(node)
        this.heartani(tween);
    }

    /**喊下动画 */
    playRespiratoryAni(node: Node) {
        let tween = this.getTween(node)
            tween
                .to(0.8, { scale: v3(1.05, 1.05, 1.05) })
                .to(0.8, { scale: v3(0.95, 0.95, 0.95) })
                .to(0.8, { scale: v3(1, 1, 1) })
                .union().repeatForever()
                .start();
    }
    //心跳动画
    heartani(tween: Tween<Node>) {
        tween
            .to(0.3, { scale: v3(1.04, 1.04, 1.04) })
            .to(0.2, { scale: v3(0.98, 0.98, 0.98) })
            .delay(0.4)
            .to(0.1, { scale: v3(1.05, 1.05, 1.05) })
            .to(0.2, { scale: v3(0.95, 0.95, 0.95) })
            .union().repeatForever()
            .start();
    }

    /**打开动画 */
    popOpenAin(tween: Tween<Node>, call?: Function) {
        tween
            .to(0.1, { scale: v3(0.9, 0.9, 0.9) })
            .to(0.05, { scale: v3(1.1, 1.1, 1.1) })
            .to(0.1, { scale: v3(1, 1, 1) })
            .call(() => {
                if (call) {
                    call();
                }
            })
            .start();
    }
    /**关闭动画 */
    popCloseAin(tween: Tween<Node>, call?: Function) {
        tween.stop();
        tween
            .to(0.1, { scale: v3(0, 0, 0) })
            .call(() => {
                if (call) {
                    call();
                }
            })
            .start();
    }
    /**重置动画 */
    resAin(tween: Tween<Node>, call?: Function) {
        tween.stop();
        tween
            .to(0.1, { scale: v3(1, 1, 1) })
            .call(() => {
                if (call) {
                    call();
                }
            })
            .start();
    }
    /**盖章 */
    stampAni(node: Node) {
        node.scale = node.scale.add(v3(1, 1, 1));
        tween(node).to(0.2, { scale: v3(1, 1, 1) }).start();
    }

    anglesAni(node: Node, count: number = 1) {
        let tween: Tween<Node> = this.getTween(node);
        node.angle = 0;
        tween.stop();
        tween
            .by(.5, { angle: 20 })
            .by(.5, { angle: -20 })
            .delay(0.5)
            .by(0.2, { angle: 10 })
            .by(0.2, { angle: -10 })
            .delay(0.1)
            .union()
        if (count == -1) {
            tween.repeatForever()
        } else {
            tween.repeat(count)
        }
        tween.start();
    }

    jumpAniForever(node: Node, delay: number = 0.2) {
        let tween = this.getTween(node)
        tween.by(0.2, { position: new Vec3(0, -10, 0) })
            .by(0.2, { position: new Vec3(0, 10, 0) })
            .delay(delay)
            .union().repeatForever()
        tween.start();

    }

}